package info.niwota.webime;

import info.niwota.webime.http.Debug;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

public class WebInputActivity extends Activity implements OnClickListener {
	private static final String TAG = "WebInputActivity";

	private Handler inputHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				CharSequence t = (CharSequence) msg.obj;
				int act = msg.arg1;
				onText(t);
				//
				if (act == EditorInfo.IME_ACTION_DONE
						|| act == EditorInfo.IME_ACTION_GO
						|| act == EditorInfo.IME_ACTION_NEXT
						|| act == EditorInfo.IME_ACTION_SEARCH
						|| act == EditorInfo.IME_ACTION_SEND) {
					performEditorAction(act);
				} else if (act == WebKeyboard.ACTION_CLEAR) {
					doClear();
				} else if (act == WebKeyboard.ACTION_RESET) {
					doClear();
				}
				break;
			case 2:
				// key event
				break;
			default:
			}
		}

		private void onText(CharSequence t) {
			text.append(t);
		}
	};

	private EditText text;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setFullScreen();

		setContentView(R.layout.main);

		initRes();
	}

	private void performEditorAction(int act) {
		// ignore for now
	}

	private void initRes() {
		text = (EditText) findViewById(R.id.input_text);
		//
		findViewById(R.id.input_title).setOnClickListener(this);
		findViewById(R.id.input_clear).setOnClickListener(this);
		findViewById(R.id.input_copy).setOnClickListener(this);
		findViewById(R.id.input_quit).setOnClickListener(this);
		findViewById(R.id.input_settings).setOnClickListener(this);
	}

	@Override
	protected void onPause() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause");
		}
		WebInputApplication appctx = (WebInputApplication) getApplicationContext();
		appctx.pad = null;

		super.onPause();
	}

	@Override
	protected void onResume() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume");
		}
		WebInputApplication appctx = (WebInputApplication) getApplicationContext();
		appctx.pad = this;

		super.onResume();
	}

	private void setFullScreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.input_clear:
			doClear();
			break;
		case R.id.input_copy:
			doCopy();
			break;
		case R.id.input_settings:
			doSettings();
			break;
		case R.id.input_quit:
			doQuit();
			break;
		case R.id.input_title:
			doHelp();
			break;
		}
	}

	private void doHelp() {
		Utils.help(this);
	}

	private void doSettings() {
		try {
			Intent i = new Intent(this, SettingsActivity.class);
			startActivity(i);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doCopy() {
		String s = text.getText().toString();
		if (!TextUtils.isEmpty(s)) {
			Utils.copyToClipboard(this, s);
		}
	}

	private void doClear() {
		text.setText("");
	}

	public boolean updateInput(int what, CharSequence text) {
		Message msg = Message.obtain(inputHandler);
		msg.what = what;
		msg.obj = text;
		return inputHandler.sendMessage(msg);
	}

	private void doQuit() {
		Utils.removeNotification(this);
		finish();
	}

}